
package com.gamatechno.solodestinationnew;

import java.io.File;
import java.util.HashMap;

import org.acra.ACRA;
import org.acra.ReportField;
import org.acra.ReportingInteractionMode;
import org.acra.annotation.ReportsCrashes;

import android.app.Application;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.text.TextUtils;

import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;
import com.gamatechno.solodestinationnew.utils.Utils;
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiscCache;
import com.nostra13.universalimageloader.cache.memory.impl.WeakMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.utils.StorageUtils;

/**
 * Base Application Class
 * 
 * @author pratama
 */

@ReportsCrashes(
        formKey = "",
        mailTo = "pratama@gamatechno.com",
        customReportContent = {
                ReportField.ANDROID_VERSION,
                ReportField.APP_VERSION_CODE,
                ReportField.PHONE_MODEL,
                ReportField.CUSTOM_DATA,
                ReportField.STACK_TRACE,
                ReportField.LOGCAT
        },
        mode = ReportingInteractionMode.SILENT)
public class BaseApplication extends Application {

    // set default timeout
    // if server not response after timeout, retry
    private static final int TIMEOUT_MS = 25000; // 5second

    private RequestQueue requestQueue;
    private static BaseApplication instance;

    @Override
    public void onCreate() {
        super.onCreate();

        File cacheDir =
                StorageUtils.getCacheDirectory(getApplicationContext());

        int availableMemory = (int) Runtime.getRuntime().maxMemory() / 1048576;

        ImageLoaderConfiguration configuration = new
                ImageLoaderConfiguration.Builder(
                        getApplicationContext())
                        .discCacheExtraOptions(480, 800, CompressFormat.PNG, 75, null)
                        .discCache(new UnlimitedDiscCache(cacheDir))
                        .discCacheFileCount(50)
                        .writeDebugLogs()
                        .memoryCache(new WeakMemoryCache())
                        .memoryCacheSize((availableMemory / 2) * 1024 * 1024)
                        .threadPoolSize(1)
                        .build();

        ImageLoader.getInstance().init(configuration);

        instance = this;

        if (!TextUtils.isEmpty(Utils
                .getDataStringFromSP(getApplicationContext(), Utils.PREF_BAHASA)))
            Utils.setLocale(getApplicationContext(),
                    Utils.getDataStringFromSP(getApplicationContext(), Utils.PREF_BAHASA));
        else
            Utils.TRACE("BaseApplication", "onCreate : data kosong");

        ACRA.init(this);
        HashMap<String, String> ACRAdata = new HashMap<String, String>();
        ACRAdata.put("my_app_info", "SoloDestination Error Report");
        ACRA.getErrorReporter().setReportSender(new ACRAPostSender(ACRAdata));
    }

    public static synchronized BaseApplication getInstance() {
        return instance;
    }

    /**
     * Get RequestQueue
     * 
     * @return
     */
    public RequestQueue getRequestQueue() {
        Utils.TRACE("BaseApplication", "getRequestQueue : ");
        if (requestQueue == null) {
            Utils.TRACE("BaseApplication", "getRequestQueue : make new instance ");
            requestQueue = Volley.newRequestQueue(getApplicationContext());
        }
        return requestQueue;
    }

    /**
     * add to request using tag
     * 
     * @param request
     * @param tag
     */
    public <T> void addToRequestQueue(Request<T> request, String tag) {
        request.setTag(tag);

        // set retry policy
        request.setRetryPolicy(new DefaultRetryPolicy(
                TIMEOUT_MS,
                DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

        Utils.TRACE("BaseApplication", "addToRequestQueue : " + request.getUrl());
        getRequestQueue().add(request);
    }

    public void cancelPendingRequest(Object tag) {
        if (requestQueue != null)
            requestQueue.cancelAll(tag);
    }

    public static DisplayImageOptions getDisplayImageOptions() {
        DisplayImageOptions options = new DisplayImageOptions.Builder()
                .cacheOnDisc(true)
                .imageScaleType(ImageScaleType.IN_SAMPLE_INT)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .build();
        return options;
    }
}
